using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using umbraco;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.interfaces;
using umbraco.presentation.cache;
using umbraco.uicontrols;
using umbraco.controls;
using Commerce.Common;
using System.Text.RegularExpressions;

namespace CommerceForUmbraco.adminSection.commerce
{
    public partial class editMailer : umbraco.BasePages.UmbracoEnsuredPage
    {
        #region properties

        public TabPage MainTab;
        public TabPage ImageTab;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string mailerId = Request.QueryString.Get("mailer");
                MessagingMailer thisMailer = new MessagingMailer(int.Parse(mailerId));
                MailerName.Text = thisMailer.MailerName;
                toList.Text = thisMailer.ToList;
                ccList.Text = thisMailer.CcList;
                fromName.Text = thisMailer.FromName;
                fromEmail.Text = thisMailer.FromEmail;
                isHtml.Checked = thisMailer.IsHTML;
                subject.Text = thisMailer.Subject;
                messageBody.Text = thisMailer.MessageBody;
            }

            // Main Tab Labels
            this.Pane1.Text = "Mailer Information";
            this.pp_name.Text = "Mailer Name";
            this.pp_to_list.Text = "To List<br /><small>Separate emails with a semicolon (;)<br /><br />Emails sent to customers will use the customer email as the To so this can be left blank.</small>";
            this.pp_cc_list.Text = "CC List";
            this.pp_from_name.Text = "From Name";
            this.pp_from_email.Text = "From Email";
            this.pp_is_html.Text = "HTML Email";
            this.pp_subject.Text = "Subject";
            this.pp_message_body.Text = "Message Body<br /><small>Replacement Variables:<br /><br />#NAME#<br />#SHIPPINGINFO#<br />#BILLINGINFO#<br />#ORDERITEMS#<br />#ORDERNUMBER#<br />#TOTAL#<br />#TRACKINGNUMBER#<br />#STOREEMAIL#<br />#TAGLINE#<br />#LINK#<br />#ORDERLINK#</small>";

            // Test Tab Labels
            this.Pane2.Text = "Test This Mailer";
            this.pp_testEmail.Text = "Test Mailer<br /><small>NOTE: the email will not have the replacements added</small>";
        }

        private void InitializeComponent()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            // Tab setup
            MainTab = tabControl.NewTabPage("Mailer");
            MainTab.Controls.Add(Pane1);

            ImageTab = tabControl.NewTabPage("Test");
            ImageTab.Controls.Add(Pane2);

            ImageButton button = MainTab.Menu.NewImageButton();
            button.Click += new ImageClickEventHandler(this.save_click);
            button.AlternateText = "Save";
            button.ImageUrl = GlobalSettings.Path + "/images/editor/save.gif";
            base.Load += new EventHandler(this.Page_Load);
            this.InitializeComponent();
            base.OnInit(e);


            this.testEmailSend.Click += new EventHandler(this.testEmailSend_Click);
        }

        private void save_click(object sender, ImageClickEventArgs e)
        {
            if (this.SaveMailer(base.Request.QueryString["mailer"]))
            {
                base.ClientTools.ShowSpeechBubble(0, ui.Text("speechBubbles", "commerceItemSaved"), "");
            }
            else
            {
                base.ClientTools.ShowSpeechBubble(0, ui.Text("speechBubbles", "commerceItemErrorHeader"), ui.Text("speechBubbles", "commerceItemErrorText"));
            }
        }

        private bool SaveMailer(string mailerId)
        {
            try
            {
                MessagingMailer thisMailer = new MessagingMailer(int.Parse(mailerId));
                thisMailer.MailerName = this.MailerName.Text;
                thisMailer.ToList = this.toList.Text;
                thisMailer.CcList = this.ccList.Text;
                thisMailer.FromName = this.fromName.Text;
                thisMailer.FromEmail = this.fromEmail.Text;
                thisMailer.IsHTML = this.isHtml.Checked;
                thisMailer.Subject = this.subject.Text;
                thisMailer.MessageBody = this.messageBody.Text;

                thisMailer.Save();

                return true;
            }
            catch
            {
                return false;
            }
        }


        private void testEmailSend_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.testToEmail.Text))
            {
                int mailerId = Int32.Parse(Request.QueryString.Get("mailer"));
                string errorMessage = "";
                //test email
                bool result = MessagingController.SendTestEmail(this.testToEmail.Text, mailerId, out errorMessage);

                if (result)
                {
                    base.ClientTools.ShowSpeechBubble(0, "Message Sent", "");
                }
                else
                {
                    base.ClientTools.ShowSpeechBubble(0, ui.Text("speechBubbles", "commerceItemErrorHeader"), "Error sending test email");
                    testMessage.Text = errorMessage;
                }
            }
        }
    }
}
